<template>
  <div class="list" ref="wrapper">
    <div>

      <div class="area">
        <div class="title">当前城市</div>
        <div class="button-list">
          <div class="button-wrapper">
            <div class="button">{{this.currentCity}}</div>
          </div>
        </div>
      </div>
      <div class="area">
        <div class="title">热门城市</div>
        <div class="button-list">
          <div class="button-wrapper" v-for="city of hotCities" :key="city.id" @click="handleClick(city.name)">
            <div class="button">{{city.name}}</div>
          </div>
        </div>
      </div>

      <div class="area" v-for="(items,key) of cities" :key="key" :ref="key">
        <div class="title">{{key}}</div>

        <div class="item-list" v-for="city of items" :key="city.id">
          <div class="item" @click="handleClick(city.name)">{{city.name}}</div>
        </div>

      </div>

    </div>
  </div>
</template>

<script>
  import BScroll from 'better-scroll'
  import {mapState, mapMutations} from 'vuex'

  export default {
    name: 'CityList',
    props: {
      cities: Object,
      hotCities: Array,
      checkLetter: String
    },
    computed: {
      ...mapState({
        currentCity: 'city'
      })
    },
    methods: {
      handleClick: function (name) {
        // this.$store.dispatch('changeCity',name)
        // this.$store.commit('changeCity', name)
        this.changeCity(name)
        this.$router.push('/')
      },
      ...mapMutations(['changeCity'])
    },
    mounted: function () {
      this.scroll = new BScroll(this.$refs.wrapper)
    },
    watch: {
      //当letter发生改变的时候会监听到
      checkLetter: function () {
        if (this.checkLetter) {
          //获取到某一个元素
          const element = this.$refs[this.checkLetter][0]
          //滚动
          this.scroll.scrollToElement(element)
        }
      }
    }
  }
</script>

<style lang="stylus" scoped>
  .list {
    overflow: hidden;
    position: absolute;
    top: 1.58rem;
    left: 0;
    bottom: 0;
    right: 0;
  }

  .title {
    line-height: .54rem;
    background: #eee;
    padding-left: .2rem;
    color: #666;
    font-size: .26rem;
    border-bottom: .005rem solid #ccc;
    border-top: .005rem solid #ccc;
  }

  .button-list {
    padding: .1rem .6rem .1rem .1rem;
    overflow: hidden;
  }

  .button-wrapper {
    float: left;
    width: 33.33%;
  }

  .button {
    text-align: center;
    margin: .1rem;
    border: .02rem solid #ccc;
    padding: .1rem 0;
    border-radius: .06rem;
  }

  .item-list {

  }

  .item {
    line-height: .76rem;
    padding-left: .2rem;
    border-bottom: .01rem solid #ccc;
  }
</style>
